YUI().use('node', function(Y) {

			//declare and initialize variables
			
			var Event = YAHOO.util.Event,
				Dom = YAHOO.util.Dom;
			
			var theInput = Y.one("#do_list" );

			var el = new YAHOO.util.Element('lists');
			
			var list = Y.one("#lists");	

			var delList = Y.one("#deleteList");

			var remove = Y.one("#remove");

			var theAddBtn = Y.one("#AddBtn" );

			var ta = Y.one("#tips" );

			var note = Y.all("todo_notes");

			var Exists;
			
			var trail = 0;
			
			printOption();
			
			function updateList( e ){

					//add new items to do to list
					var value = trim(theInput.get('value'));

					//var li_id = el.get('childNodes').length + 1;
					
					var li_id = trail + 1;

					//if value is not empty insert it into task list
					if (value != null && value != "" && value.length != 0) { 

						//check if text to do item already exists
						isExists(value);
						if(!Exists) {
							var val = '<div id="Clist_item'+li_id+'" class="Clist_items">'+
									  '<span class="todo_text" id="todo_text'+li_id+'">' + value + '</span>'+ 
									  '<span class="liNote" id="notes_list_item'+li_id+'"></span>'+
									  '<ul class="todo_items" id="ul_list_item'+li_id+'">'+
									  '<li class="todo_notes"  title="add notes" id="todo_notes'+li_id+'"><a href="#" id="notes" title="notes"></a></li>'+
									  '<li class="done_todo" title="finished" id="done'+li_id+'"><a href="#" id="done" title="done"></a></li>'+								  
									  '<li class="todo_important" title="important task" id="impt'+li_id+'"><a href="#" id="important" title="important"></a></li>'+
									  '<li class="todo_delete" title="delete" id="del'+li_id+'"><a href="#" title="delete"></a></li>'+
									  '</ul></div>'
							var li_item = '<li id ="list_item'+li_id+'" class="list_item">' + val + '</li>';
		
							list.appendChild(li_item);
							
							trail++;
							
							hideErr();
														
						} else {
							//alert("this items already exists in list.");
							document.getElementById("error").style.display = 'block';
						}

						clearText();

						printOption();
						
						if (YAHOO.env.ua.ie > 0) {
							document.getElementById("do_list").focus();
						}
					}		
			}
			
			function hideErr(e) {
				document.getElementById("error").style.display = 'none';
			}

			//check if text to do item already exists
			function isExists( val ){	
				Exists = false;
				if (el.hasChildNodes()) {	
					nons = Y.all('.todo_text');		
					nons.each(function(n) {
						var txt = trim(document.getElementById(n.get('id')).innerHTML);
						if (txt.toLowerCase() == val.toLowerCase()) {
							Exists = true;							
						}						
					});
				}				
			}

			///show print icon only if to do list exists
			function printOption( e ){				
				if (list.hasChildNodes()) {
					document.getElementById("printList").style.display = 'inline'; 
					document.getElementById("todo_status").style.display = 'block';					
		        } else {
		        	document.getElementById("printList").style.display = 'none';
		        	clearStatus();
				}	
				document.getElementById("help").style.display = "inline";
			}

			//clear to do list status panel
			function clearStatus( e ){				
	        	document.getElementById("todo_status").style.display = 'none';
	        	document.getElementById("CL").style.display = 'none';
	        	document.getElementById("CT").innerHTML = "";
			}

			//clear the last value of input box
			function clearText(e) {
				theInput.set('value', "");
			}

			//add new item to the to-do list 			
			var keyObj = {keys:13};
			var keyHandle = new YAHOO.util.KeyListener('do_list', keyObj, updateList);
			keyHandle.enable();
			
			//clear old text if any in text box before typing
			//theInput.on('focus', clearText);
			theInput.on("click", function (e) {
				theInput.set('value', "");
				hideErr();
			});

			
			//clear do to list after confirmation
			delList.on("click", function (e) {
				var con = confirm("Are you sure to clear to do list?");
				if(con){
					document.getElementById("lists").innerHTML = "";
					printOption();
					clearStatus();
					clearText();
				} 
			});

			remove.on("click", function (e) {
					document.getElementById("CT").innerHTML = "";
					document.getElementById("todo_status").style.display = 'none';

					nons = Y.all('.list_item');
					nons.each(function(n) {
						if (n.hasClass('strikethru')) {
							n.remove();
						}
						clearStatus();
					});
			});

		//editor to add notes to to do lists
		function updateNotes( pEID ){
			YAHOO.util.Dom.addClass('editor-container', 'editor-visible');
			document.getElementById('tips').focus();
			
			//get the id of list item
			document.getElementById("tarID").value = pEID;

			//get old value into add notes input
			var noteval = document.getElementById('notes_'+pEID).innerHTML
			
			if(noteval != "") {
				noteval = noteval.replace("Note: ","");
			}
			document.getElementById('tips').value = noteval;
		}

		//add notes to the to-do list 
		theAddBtn.on("click", function (e) {
			var value = trim(document.getElementById('tips').value);

			if (value != null && value != "" && value.length != 0) { 
				
				pEID = trim(document.getElementById("tarID").value);
				document.getElementById('notes_'+pEID).innerHTML = "Note: " + value;

				var item = Y.Node.create('<span  title="see notes" id="note'+pEID+'" class="note"><img src="images/info-icon.png" border="0"></span>');
				var fChild	= (document.getElementById(pEID).firstChild).id;
				var parUL = Y.one("#"+fChild);
				var noteField = YAHOO.util.Dom.getElementsByClassName('liNote', 'span',fChild);

				if(YAHOO.util.Dom.getElementsByClassName('note', 'span',fChild) == "") { 
					parUL.insertBefore(item,noteField);
				}
			}

			YAHOO.util.Dom.removeClass('editor-container', 'editor-visible');
			YAHOO.util.Dom.addClass('editor-container', 'editor-container');

			document.getElementById('tips').value = "";
		});
		
		//hide note editor if document is clicked
		function hideTA(e) {
			var elem = Event.getTarget(e);
			if(YAHOO.util.Dom.hasClass(elem,'todo_notes') || YAHOO.util.Dom.hasClass(elem,'ta') || YAHOO.util.Dom.hasClass(elem,'AddBtn')) {
				/*alert(elem);
				var pEID = elem.parentNode.parentNode.parentNode.id;
				
				updateNotes(pEID);	*/		
			} else {
				YAHOO.util.Dom.removeClass('editor-container', 'editor-visible');
				YAHOO.util.Dom.addClass('editor-container', 'editor-container');
				if(YAHOO.util.Dom.hasClass(elem,'note')) {
					document.getElementById("lightbox").style.display = "none";
					document.getElementById("lightbox-panel").style.display = "none";
				}
			}
		}

		Y.one('document').delegate('click', hideTA, '#todo_holder');
	
		//handle add note, highlight, delete, done tasks
	    var onClick = function(e) {
	    	var oElement = this._node;

	    	//get parent 'li' element's id
	    	try {
		    	//var pEID = (YAHOO.util.Dom.getAncestorByTagName(oElement,"li")).id;
		        var pEID = oElement.parentNode.parentNode.parentNode.id;
	    	} catch(e) {
	    		var pEID = 0;
	    	}

	        if (YAHOO.util.Dom.hasClass(oElement,"done_todo")) {
		      if(YAHOO.util.Dom.hasClass(pEID, 'strikethru')){
		    	YAHOO.util.Dom.removeClass(pEID, 'strikethru'); 
		      }
		      else {      
				YAHOO.util.Dom.addClass(pEID, 'strikethru');     
		      }
	        }
	        if (YAHOO.util.Dom.hasClass(oElement,"todo_important")) {
			      if(YAHOO.util.Dom.hasClass(pEID, 'highlight')){
			    	YAHOO.util.Dom.removeClass(pEID, 'highlight'); 
			      }
			      else {      
					YAHOO.util.Dom.addClass(pEID, 'highlight');     
			      }
		    }
	        if (YAHOO.util.Dom.hasClass(oElement,"todo_notes")) {
	        	updateNotes(pEID);
		    }
	        if (YAHOO.util.Dom.hasClass(oElement,"todo_delete")) {

	  		  var obj = document.getElementById(pEID);
	  		  var d = document.getElementById('lists');
	  		  
	  		  try {
	      	  	//obj.parentNode.removeChild(pEID);
	      	  	d.removeChild(obj);      	  	
	  		  }
	  		  catch(e) {
		  		 obj.style.display = 'none'; 
	          }
	        }
			//list of finished tasks
	        var tasksCompleted = YAHOO.util.Dom.getElementsByClassName('strikethru', 'li').length

	        if (tasksCompleted > 0) {
				document.getElementById("CT").innerHTML = tasksCompleted + " tasks completed."
				document.getElementById("todo_status").style.display = 'block'; 
				document.getElementById("CL").style.display = 'inline';
	        } else {
	        	clearStatus();
	        }

			printOption();
	    };
	    
	    Y.one('#lists').delegate('click', onClick, 'li');
	    
	    //display comments of the task
		function displayNotes(e) {
			var oElement = this._node;
			var noteId = oElement.id.replace("note","notes_");
			var close = "<a href='#' title='Close' class='close'><img src='images/close_icon.png' border='0'></a>";
			var htm = close + document.getElementById(noteId).innerHTML;
			document.getElementById("lightbox-panel").innerHTML = htm;
			document.getElementById("lightbox").style.display = "block";
			document.getElementById("lightbox-panel").style.display = "block";
			//YAHOO.util.Dom.removeClass(noteId, 'liNote');
			//YAHOO.util.Dom.addClass(noteId, 'liNoteShow');
			
		}

	    Y.delegate("click", displayNotes, "#lists", ".note");
	    
	    function closePanel(e) {
	    	document.getElementById("lightbox").style.display = "none";
			document.getElementById("lightbox-panel").style.display = "none";
	    }
	    
	    Y.delegate("click", closePanel, "#lightbox-panel", ".close");
});

//Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

//Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}
//Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}